using Manon.Autofac;
using Manon.Gateway.Api.Common;
using Manon.Gateway.ApplicationCore.Infrastructure;
using Manon.Repository;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Consul;
using Ocelot.Provider.Consul;

namespace Manon.Gateway.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        { 
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "Manon.Gateway.Api", Version = "v1" });
            });
         
            services.AddOcelot().AddManonOcelot(op =>
            {

            }).AddConsul();

            services.AddUnitOfWorkService<GatewayDbContext>(options =>
            {
                options.UseMySQL("server=rm-8vbu2720v5g6p48wu4o.mysql.zhangbei.rds.aliyuncs.com;port=3306;userid=zzw_2020;password=zzw@2020;database=gateway;TreatTinyAsBoolean=false;Persist Security Info=True;");
            });


            services.AddHttpContextAccessor();

            services.RegisterAutofacService("Manon.Gateway.Api", "Manon.Gateway.ApplicationCore");
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Manon.Gateway.Api v1"));
            }
           
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseOcelot().Wait();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
